package com.cxrj.zhsq.community.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;

import com.cxrj.zhsq.community.dao.CommunityCircleMapper;
import com.cxrj.zhsq.community.dao.CommunityComplaintMapper;
import com.cxrj.zhsq.community.dao.CommunityReplyMapper;
import com.cxrj.zhsq.community.entity.CommunityCircleWithBLOBs;
import com.cxrj.zhsq.community.entity.CommunityComplaint;
import com.cxrj.zhsq.community.entity.CommunityReply;
import com.cxrj.zhsq.community.service.CommunityService;
import com.cxrj.zhsq.userManage.dao.AppUserBlackMapper;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.userManage.entity.AppUserBlack;

@Service
public class CommunityServiceImpl implements CommunityService {

	@Resource
	private CommunityCircleMapper communityCircleMapper;

	@Resource
	private CommunityReplyMapper communityReplyMapper;

	@Resource
	private AppUserMapper appUserMapper;

	@Resource
	private CommunityComplaintMapper communityComplaintMapper;

	@Resource
	private AppUserBlackMapper appUserBlackMapper;

	@Override
	public Map<String, Object> insertCommunityCircle(String userId, String content, String pics) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		CommunityCircleWithBLOBs communityCircleWithBLOBs = new CommunityCircleWithBLOBs();
		communityCircleWithBLOBs.setId(UUID.randomUUID().toString());
		communityCircleWithBLOBs.setCreateTime(new Date());
		communityCircleWithBLOBs.setS1("1");
		communityCircleWithBLOBs.setContent(content);
		communityCircleWithBLOBs.setUserId(userId);
		communityCircleWithBLOBs.setUserPicture(appUser.getUserPicture());
		communityCircleWithBLOBs.setUserName(appUser.getUserName());
		if (!pics.equals("")) {
			communityCircleWithBLOBs.setPicture(pics.substring(1));
		}
		Integer c = communityCircleMapper.insertSelective(communityCircleWithBLOBs);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertCommunityCircleByBackstage(HttpServletRequest request, String content, String pics) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		CommunityCircleWithBLOBs communityCircleWithBLOBs = new CommunityCircleWithBLOBs();
		communityCircleWithBLOBs.setId(UUID.randomUUID().toString());
		communityCircleWithBLOBs.setCreateTime(new Date());
		communityCircleWithBLOBs.setS1("1");
		communityCircleWithBLOBs.setContent(content);
		communityCircleWithBLOBs.setUserId(request.getSession().getAttribute("userId").toString());
		communityCircleWithBLOBs.setUserName(request.getSession().getAttribute("userName").toString());
		try {
			File file = new File(request.getSession().getServletContext().getRealPath("/") + "img/user.png");
			String fileName = file.getName();
			String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			byte[] temp = bos.toByteArray();
			communityCircleWithBLOBs.setUserPicture("data:image/" + suffix + ";base64," + Base64.encodeBase64String(temp));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (!pics.equals("")) {
			communityCircleWithBLOBs.setPicture(pics.substring(1));
		}
		Integer c = communityCircleMapper.insertSelective(communityCircleWithBLOBs);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommunityCircle(String userId, String dataIndex, String pageCount, String userId1) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		sqlMap.put("userId", userId);
		List<String> strs = new ArrayList<String>();
		List<AppUserBlack> appUserBlacks = appUserBlackMapper.selectAppUserBlack(userId1);
		for (AppUserBlack appUserBlack : appUserBlacks) {
			strs.add(appUserBlack.getUserBlackId());
		}
		if (strs.size() != 0) {
			sqlMap.put("isList", "1");
			sqlMap.put("list", strs);
		}
		List<CommunityCircleWithBLOBs> list = communityCircleMapper.selectCommunityCircle(sqlMap);
		for (CommunityCircleWithBLOBs communityCircleWithBLOBs : list) {
			communityCircleWithBLOBs.setCommunityReplies(communityReplyMapper.selectByCircleId(communityCircleWithBLOBs.getId()));
		}
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertCommunityReply(String id, String userName, String replyUserName, String content) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String replyId = UUID.randomUUID().toString();
		CommunityReply communityReply = new CommunityReply();
		communityReply.setCircleId(id);
		communityReply.setId(replyId);
		communityReply.setReplyContent(content);
		communityReply.setReplyName(userName);
		communityReply.setReplyObject(replyUserName);
		communityReply.setReplyTime(new Date());
		communityReply.setS1("1");
		Integer c = communityReplyMapper.insertSelective(communityReply);
		resultMap.put("result", 0);
		resultMap.put("replyId", replyId);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommunityReplyById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("replys", communityReplyMapper.selectByCircleId(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommunityCirclePager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("start", Integer.parseInt(start));
		// sqlMap.put("condition", " where" + str);
		sqlMap.put("length", Integer.parseInt(length));

		// 获取数据库总记录数
		recordsTotal = communityCircleMapper.getCommunityCircleCount(sqlMap) + "";

		List<Map<String, Object>> list = communityCircleMapper.selectCommunityCirclePager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommunityCircleById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("circle", communityCircleMapper.selectByPrimaryKey(id));
		resultMap.put("list", communityReplyMapper.selectByCircleId(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteCommunityCircle(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		communityCircleMapper.deleteCommunityCircle(id);
		resultMap.put("result", 1);
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteCommunityReply(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		CommunityReply reply = communityReplyMapper.selectByPrimaryKey(id);
		reply.setS1("-1");
		Integer c = communityReplyMapper.updateByPrimaryKeySelective(reply);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertCommunityComplaint(String userId, String text, String circleId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		CommunityComplaint communityComplaint = new CommunityComplaint();
		communityComplaint.setId(UUID.randomUUID().toString());
		communityComplaint.setCircleId(circleId);
		communityComplaint.setText(text);
		communityComplaint.setCreateTime(new Date());
		communityComplaint.setUserId(userId);
		Integer c = communityComplaintMapper.insertSelective(communityComplaint);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommunityComplaint(String circleId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<CommunityComplaint> list = communityComplaintMapper.selectCommunityComplaint(circleId);
		resultMap.put("list", list);
		return resultMap;
	}
}
